import request from '@/router/axios';

/*export const exchangeToken = (data, success, error) => {
  $.ajax({
    url: '/google-api/token',
    type: 'post',
    data: JSON.stringify(data),
    headers: {
      "Connection": "keep-alive",
      'Content-Type': 'application/json;charset=UTF-8'
    },
    timeout: 15000,
    success: (res) => success(res),
    error: (res) => error(res)
  })
}*/

export const exchangeToken = (data, success, error, cb) => {
  let xhr = new XMLHttpRequest();
  xhr.onprogress = function(event){
    console.log( event.loaded / event.total );
  }
  xhr.onload = success
  xhr.onerror = error
  xhr.timeout = 15000;
  xhr.ontimeout = error
  xhr.open('POST', 'https://oauth2.googleapis.com/token');
  xhr.send(JSON.stringify(data));
  cb&&cb(xhr)
}

export const revokeToken = (url, success, error) => {
  let xhr = new XMLHttpRequest();
  xhr.onload = success
  xhr.onerror = error
  xhr.timeout = 10000;
  xhr.ontimeout = error
  xhr.open('POST', url);
  xhr.send();
}

export const getChannelInfo = (url, success, error) => {
  let xhr = new XMLHttpRequest();
  xhr.onload = success
  xhr.onerror = error
  xhr.timeout = 15000;
  xhr.ontimeout = error
  xhr.open('GET', url);
  xhr.send();
}

export const getGrantInfo = () => {
  return request({
    url: '/api/beetles-business/youtube-config/list',
    method: 'get'
  })
}

export const removeGrant = (ids) => {
  return request({
    url: '/api/beetles-business/youtube-config/remove-grant',
    method: 'delete',
    params: {ids}
  })
}

export const updateGrant = (data) => {
  return request({
    url: '/api/beetles-business/youtube-config/save',
    method: 'post',
    data: data
  })
}

export const getChannelSelect = () => {
  return request({
    url: '/api/beetles-business/youtube-config/channel-select',
    method: 'get'
  })
}
